// pages/me/me.js
var util = require('../../utils/util.js');
Page({

  data: {
    userInfo: "",
    sortList: [{
        sortName: '待付款',
        sortUrl: '/weixinImg/dfk_icon@2x.png',
        orderCount: ''
      },
      {
        sortName: '待配送',
        sortUrl: '/weixinImg/dps_icon@2x.png',
        orderCount: ''
      },
      {
        sortName: '待确认',
        sortUrl: '/weixinImg/dqr_icon@2x.png',
        orderCount: ''
      },
      {
        sortName: '已完成',
        sortUrl: '/weixinImg/ywc_icon@2x.png',
        orderCount: ''
      },
      {
        sortName: '退换/售后',
        sortUrl: '/weixinImg/sh_icon@2x.png',
        orderCount: ''
      }
    ],
    logistics: '',
    send_state: '', //配送状态
    goods_img: '',
    active_time: '',
    token: "",
  },
  onLoad: function(options) {
    var token = wx.getStorageSync('token');
    if (token != "") {
      this.setData({
        orderCount: true,
        token: token,
      })
    } else {
      this.setData({
        orderCount: false,
      })
    }
    this.setData({
      userInfo: wx.getStorageSync("userInfo")
    })
  },
  onShow: function() {
    var that = this;
    that.memberInfo();
  },
  onShareAppMessage: function() {

  },
  bindGetUserInfo: function(res) {
    var that = this;
    if (wx.getStorageSync('token') != "") {
      return;
    }
    wx.showLoading({
      title: '正在授权中..',
    })
    var temp_goods = wx.getStorageSync('temp_goods');
    wx.login({
      success: function(login_res) {
        if (login_res.code) {

          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {

              util.postUrl("connect/weixin_code_login", {
                user_code: login_res.code,
                cookie_cart: JSON.stringify(temp_goods),
              }, function (result) {
                if (result.data.code != 200) {
                  wx.showToast({
                    title: '获取失败',
                  })
                  return;
                }
                wx.setStorageSync("open_id", result.data.datas.open_id);
                if (result.data.datas.user_token != '') {
                  console.log("userInfo", res);
                  wx.setStorageSync("userInfo", res);
                  wx.setStorageSync("token", result.data.datas.user_token)
                  that.memberInfo();
                  that.setData({
                    userInfo: wx.getStorageSync("userInfo"),
                    token: result.data.datas.user_token,
                  })
                  wx.hideLoading();
                  wx.setStorageSync("temp_goods", "");
                  if (result.data.datas.bind_phone == '0') {
                    wx.navigateTo({
                      url: '../register/register',
                    })
                  }
                } else {
                  util.postUrl("connect/weixin_iv_login", {
                    session_key: result.data.datas.session_key,
                    encrypted_data: res.encryptedData,
                    iv: res.iv,
                    cookie_cart: JSON.stringify(temp_goods),
                  }, function (res1) {
                    wx.hideLoading();
                    console.log("userInfo", res);
                    wx.setStorageSync("userInfo", res);
                    that.memberInfo();
                    wx.setStorageSync("token", res1.data.datas.user_token);
                    that.setData({
                      userInfo: wx.getStorageSync("userInfo"),
                      token: res1.data.datas.user_token,
                    })
                    wx.setStorageSync("temp_goods", "");

                    if (res1.data.datas.bind_phone == '0') {
                      wx.navigateTo({
                        url: '../register/register',
                      })
                    }
                  })
                }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '授权失败',
                icon: "none",
              })
              wx.hideLoading();
            }
          })
        }
      }
    });
  },

  memberInfo: function() {
    var that = this;
    util.postUrl("member/info", {}, function(res) {
      if (res.data.code == 200) {
        var ans = res.data.datas.member_info;
        console.log('订单类型：', ans.orders);
        var sortList = that.data.sortList;
        sortList[0].orderCount = ans.orders.new_num;
        if (ans.orders.new_num == 0) {
          sortList[0].orderCount = false
        }
        sortList[1].orderCount = ans.orders.pre_send_num;
        sortList[2].orderCount = ans.orders.send_num;
        sortList[3].orderCount = ans.orders.success_num;
        sortList[4].orderCount = ans.orders.after_num;

        that.setData({
          sortList: sortList,
          logistics: ans.logistics.info.send_message,
          send_state: ans.logistics.info.send_state,
          goods_img: ans.logistics.info.goods_img,
          active_time: ans.logistics.info.active_time
        })
      }
    })
  },

  // 商品收藏
  goodsCollect: function() {
    wx.navigateTo({
      url: '../goodsCollect/goodsCollect',
    })
  },
  storeCollect: function() {
    wx.navigateTo({
      url: '../storeCollect/storeCollect'
    })
  },
  // 门店管理：
  shopManagement: function() {
    wx.navigateTo({
      url: '../shopManagement/shopManagement?flag=0',
    })
  },
  goMyOrder: function(e) {
    var index = e.currentTarget.dataset.index;
    if (index == 4) {
      wx.navigateTo({
        url: '../quit_list/quit_list',
      })
      return;
    }
    wx.navigateTo({
      url: '../myOrder/myOrder?currentTab=' + (parseInt(index) + 1),
    })
  },
  // 优惠券
  discount() {
    wx.navigateTo({
      url: '../myDiscount/myDiscount',
    })
  },
  // 浏览历史：
  viewHistory() {
    wx.navigateTo({
      url: '../viewHistory/viewHistory',
    })
  }
})